<?php require_once('../conn/DBconnect.php'); 

	session_start();

	$firstName = "";
 	$lastName = "";
 	$middleIni = "";
 	$gender = "";
 	$bdate = "";
 	$email = "";
 	$mobile = "";
 	$nationality = "";
 	$country_code = "";
 	$address1 = "";
 	$address2 = "";
 	$deliveryAdd = "";
 	$contactName = "";
 	$contactPhone = "";
 	
 	if (isset($_POST["r_race_code"])) 
	{
		setcookie("r_race_code", $_POST["r_race_code"], time()+3600);
	    $_SESSION['r_race_code'] = $_POST["r_race_code"];
	}
	
	if(isset($_SESSION['loggedUser']) && $_SESSION['isRegistered'] == false)
	{
		$firstName = $_SESSION["loggedUser"]["pi_first_name"];
	 	$lastName = $_SESSION["loggedUser"]["pi_last_name"];
	 	$middleIni = $_SESSION["loggedUser"]["pi_middle_initial"];
	 	$gender = $_SESSION["loggedUser"]["pi_gender"];
	 	$bdate = $_SESSION["loggedUser"]["pi_birthdate"];
	 	$email = $_SESSION["loggedUser"]["pi_email_add"];
	 	$mobile = $_SESSION["loggedUser"]["pi_mobile_no"];
	 	$nationality = $_SESSION["loggedUser"]["pi_nationality"];
	 	$country_code = $_SESSION["loggedUser"]["pi_country"];
	 	$address1 = $_SESSION["loggedUser"]["pi_address1"];
	 	$address2 = $_SESSION["loggedUser"]["pi_address2"];
	 	$deliveryAdd = $_SESSION["loggedUser"]["pi_delivery_add"];
	 	$contactName = $_SESSION["loggedUser"]["pi_contact_name"];
	 	$contactPhone = $_SESSION["loggedUser"]["pi_contact_phone"];
	}
	elseif(isset($_POST["r_firstName"]))
	{
	 	$firstName = $_POST["r_firstName"];
	 	$lastName = $_POST["r_lastName"];
	 	$middleIni = $_POST["r_middle_ini"];
	 	$gender = $_POST["r_gender"];
	 	$bdate = $_POST["r_birthdate"];
	 	$email = $_POST["r_email"];
	 	$mobile = $_POST["r_mobile"];
	 	$nationality = $_POST["r_nationality"];
	 	$country_code = $_POST["r_country"];
	 	$address1 = $_POST["r_address1"];
	 	$address2 = $_POST["r_address2"];
	 	$deliveryAdd = $_POST["r_delivery_add"];
	 	$contactName = $_POST["r_contact_name"];
	 	$contactPhone = $_POST["r_contact_phone"];
	}
	
	$registerOther = false;
	$disableEmail = "";
	if(isset($_SESSION['isRegistered']) && $_SESSION['isRegistered'] == true)
	{
		$registerOther = true;
		$disableEmail = "disabled='disabled'";
		$email = $_SESSION["loggedUser"]["pi_email_add"];
	}
?>
<div id="div-container">
	<div id="div-header">
		<div class="div-header-title">
			<div class="dh-title-left"></div>
			<div class="dh-title-center">Participant Information</div>
			<div class="dh-title-right"></div>
		</div>
	</div>
	<div id="div-content">
		<?php
		 if($registerOther)
		 {
		?>
		<div class="formItem">
			<div class="formTitleLabel" style="text-align:right;width:400px;">Register another participant under my account</div>
		</div>
		<?php }?>
		<div class="formItem">
			<div class="formLabelRight"><font color="#ff0000">*</font>Email address</div>
			<div>
			<input type="text" name="emailAdd" class="formInputEmail" maxlength="50" value="<?php echo $email?>" <?php echo $disableEmail ?>/>
			</div>
			<div id="emailErrorMsg" class="formErrorMsg" style="width: 160px; max-width: 160px;"></div>
		</div>
		<div class="formItem">
			<div class="formLabelRight"><font color="#ff0000">*</font>First name</div>
			<div><input type="text" name="firstName" class="formInputAlphabet" maxlength="50" value="<?php echo $firstName?>"/></div>
			<div id="firstNameErrorMsg" class="formErrorMsg"></div>
		</div>
		<div class="formItem">
			<div class="formLabelRight"><font color="#ff0000">*</font>Last name</div>
			<div><input type="text" name="lastName" class="formInputAlphabet" maxlength="50" value="<?php echo $lastName?>"/></div>
			<div id="lastNameErrorMsg" class="formErrorMsg"></div>
		</div>
		<div class="formItem">
			<div class="formLabelRight">Middle Initial</div>
			<div><input type="text" name="middleInitial" class="formInputAlphabet" maxlength="1" value="<?php echo $middleIni?>"/></div>
		</div>
		<div class="formItem">
			<div class="formLabelRight"><font color="#ff0000">*</font>Gender</div>
			<div>
				<div><input type="radio" name="gender" value="M" <?php if($gender == "M"){ echo "checked='checked'";} ?>></div>
				<div class="formLabel" style="width:60px;">Male</div>
				<div><input type="radio" name="gender" value="F" <?php if($gender == "F"){ echo "checked='checked'";} ?>></div>
				<div class="formLabel" style="width:60px;">Female</div>
			</div>
			<div id="genderErrorMsg" class="formErrorMsg"></div>
		</div>
		<div class="formItem">
			<div class="formLabelRight"><font color="#ff0000">*</font>Date of birth</div>
			<div><input type="text" name="birthDate" class="formInputDate" maxlength="10" value="<?php echo $bdate?>"/></div>
			<div id="birthDateErrorMsg" class="formErrorMsg"></div>
		</div>
		<div class="formItem">
			<div class="formLabelRight" style="width:200px;">&nbsp;</div>
			<div><i>format: mm/dd/yyyy</i></div>
		</div>
		<div class="formItem">
			<div class="formLabelRight">Mobile</div>
			<div><input type="text" name="mobileNo" class="formInputNumbers" maxlength="11" value="<?php echo $mobile?>"/></div>
		</div>
		<div class="formItem">
			<div class="formTitleLabel" style="text-align:right;">Your address</div>
		</div>
		<div class="formItem">
			<div class="formLabelRight"><font color="#ff0000">*</font>Home Address</div>
			<div><input type="text"  name="address1" class="formInput" maxlength="200" value="<?php echo $address1?>"/></div>
			<div id="homeAddErrorMsg" class="formErrorMsg"></div>
		</div>
		<div class="formItem">
			<div class="formLabelRight">Delivery Address</div>
			<div>
				<div><input type="radio" name="deliveryAdd" value="HOME" <?php if($deliveryAdd == "" || $deliveryAdd == "HOME"){ echo "checked='checked'";} ?>></div>
				<div class="formLabel" style="width:60px;">HOME</div>
				<div><input type="radio" name="deliveryAdd" value="ADD2" <?php if($deliveryAdd == "ADD2"){ echo "checked='checked'";} ?>></div>
				<div class="formLabel" style="width:80px;">ADDRESS 2</div>
			</div>
		</div>
		<div class="formItem">
			<div class="formLabelRight">Address 2</div>
			<div><input type="text" name="address2" class="formInput" maxlength="200" value="<?php echo $address2?>"/></div>
			<div id="add2ErrorMsg" class="formErrorMsg"></div>
		</div>
		<div class="formItem">
			<div class="formLabelRight"><font color="#ff0000">*</font>Country</div>
			<div>
				<select name="country" class="formInput">
				<?php 
		
					$countrySQL = "call sp_countries('LIST',null,null);";

					$result = mysqli_query($mysqlLink, $countrySQL);
					
					$isChecked = false;
					while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
					{
						if($country_code == "" && $row["ccode"] == "PH"){
							$isChecked = true;
						}
						else if($country_code == $row["ccode"]){
							$isChecked = true;
						}
				?>
				
					<option value="<?php echo $row["ccode"]; ?>" <?php if($isChecked){ echo "selected='selected'"; $isChecked=false; } ?> ><?php echo $row["country"]?></option>
				
				<?php
					}

					/* free result set */
					mysqli_free_result($result);
					
					/* close connection */
					mysqli_close($mysqlLink);
				?>
				</select>
			</div>
		</div>
		<div class="formItem">
			<div class="formLabelRight"><font color="#ff0000">*</font>Nationality</div>
			<div><input type="text" name="nationality" class="formInputAlphabet" maxlength="50" value="<?php echo $nationality?>"/></div>
			<div id="nationalityErrorMsg" class="formErrorMsg"></div>
		</div>
		
		<div class="formItem">
			<div class="formLabelRight">Emergency contact name</div>
			<div><input type="text" name="cantactName" class="formInputAlphabet" maxlength="100" value="<?php echo $contactName?>"/></div>
		</div>
		<div class="formItem">
			<div class="formLabelRight">Emergency contact phone</div>
			<div><input type="text" name="contactPhone" class="formInputNumbers" maxlength="15" value="<?php echo $contactPhone?>"/></div>
		</div>
	</div>
</div>
